package com.sanchunhui.psc.ui.adapter

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.sanchunhui.psc.R
import com.sanchunhui.psc.ui.activity.InformationActivity
import com.squareup.picasso.Picasso
import org.jetbrains.anko.find

/**
 * @Author zyx
 * @Date 2021/5/22-17:46
 * @Dscribe
 */

class InformationAdapter(val context: FragmentActivity?) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    val titles : List<String> = listOf("看一看","听一听","读一读","考一考")
    val sampleDrawables = intArrayOf(R.drawable.information_look, R.drawable.information_listen, R.drawable.information_read, R.drawable.information_test)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return InformationViewHolder(LayoutInflater.from(context).inflate(R.layout.item_information,parent,false))
    }

    override fun getItemCount(): Int {
        return titles.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as InformationViewHolder).bindData(titles[position], position)
    }

    inner class InformationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val titleName : TextView
        val headImage : ImageView
        init {
            titleName = itemView.find(R.id.tv_information_title)
            headImage = itemView.find(R.id.iv_information_head)
        }
        fun bindData(title: String, position: Int) {
            titleName.text = titles[position]
            headImage.setImageResource(sampleDrawables[position])
            itemView.setOnClickListener{
                val intent: Intent = Intent(context, InformationActivity::class.java)
                intent.putExtra("position", position)
                context!!.startActivity(intent)
            }
        }
    }

}